
from sklearn.datasets import fetch_openml
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score

#载入手写数字图片数据集
mnist = fetch_openml('mnist_784', version=1)

#数据预处理，标准归一化
scaler = StandardScaler()
X = scaler.fit_transform(mnist.data)

#划分数据集为训练数据集和测试集
X_train,X_test,y_train,y_test = train_test_split(X,mnist.target,test_size = 0.2)

#创建回归模型
model = LogisticRegression(max_iter=1000)

#训练模型
model.fit(X_train,y_train)

#在测试集上测试
y_pred = model.predict(X_test)

print('accuracy:%.2f' % accuracy_score(y_test,y_pred))



#显示一张图\n
import numpy as np
import matplotlib.pyplot as plt

img_0 = np.array(mnist.data)[1]
img_0 = img_0.reshape(28,28)
plt.imshow(img_0)
#最这个一个图进行预测
img_0_test = np.array(mnist.data)[1]
print(model.predict([img_0_test]))


